import { All, Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiBody,
  ApiExcludeController,
  ApiExcludeEndpoint,
  ApiOperation,
  ApiTags,
} from '@nestjs/swagger';
import { Public } from '~/modules/auth/decorators/public.decorator';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { PointsService } from '~/modules/points/points.service';
import { AuthUser } from '~/modules/auth/decorators/auth-user.decorator';
import { AllowAnon } from '~/modules/auth/decorators/allow-anon.decorator';
import { ApiResult } from '~/common/decorators/api-result.decorator';
import { PointsGoodsService } from '~/modules/points/points-goods/points-goods.service';
import { ApiTenantHeader } from '~/common/decorators/api-header-tenant.decorator';
import { AuthTenant } from '~/modules/tenant/tenant.decorator';
import { FindPointsGoodsListDto } from '~/modules/points/points-goods/points-goods.dto';
import { PointsLogService } from '~/modules/points/points-log/points-log.service';
import { Pagination } from '~/helper/paginate/pagination';
import { PointsLogEntity } from '~/modules/points/points-log/points-log.entity';
import { FindPointsLogsListDto } from '~/modules/points/points-log/points-log.dto';
import { PointsEntityWithRank } from '~/modules/points/points.model';
import { PointsGoodsExchangeService } from '~/modules/points/points-goods/points-goods-exchange/points-goods-exchange.service';
import { FindPointsGoodsExchangeListDto } from '~/modules/points/points-goods/points-goods-exchange/points-goods-exchange.dto';
import { FindPointsRankDto } from '~/modules/points/points.dto';

@ApiBearerAuth()
@ApiTags(ModuleSwaggerTags.POINTS)
@Controller('points')
export class MiniappPointsController {
  constructor(
    private readonly pointsService: PointsService,
    private readonly pointsGoodsService: PointsGoodsService,
    private readonly pointsLogService: PointsLogService,
    private readonly pointsGoodsExchangeService: PointsGoodsExchangeService
  ) {}

  // 查询推荐商品
  @Public()
  @Get('goods/recommend')
  @ApiOperation({ summary: '获取推荐商品列表' })
  @ApiTenantHeader()
  async getGoodsRecommend(@AuthTenant() tenant: IAuthTenant) {
    // 获取推荐商品列表
    return await this.pointsGoodsService.findRecommend(tenant.id);
  }

  // 查询商品列表
  @Public()
  @Get('goods/list')
  @ApiOperation({ summary: '获取商品列表' })
  @ApiTenantHeader()
  async getGoodsList(@Query() dto: FindPointsGoodsListDto, @AuthTenant() tenant: IAuthTenant) {
    // 获取商品列表
    return await this.pointsGoodsService.findByList({ ...dto, tenantId: tenant.id });
  }

	@Public()
  @Get('self')
  @ApiOperation({ summary: '获取用户积分' })
  @ApiTenantHeader()
  async getSelfPoints(
    @AuthUser() user: IAuthUser,
    @AuthTenant() tenant: IAuthTenant
  ): Promise<Partial<PointsEntityWithRank>> {
    return await this.pointsService.findByUserId(user?.uid, tenant.id);
  }

  @Get('self/list')
  @AllowAnon()
  @ApiOperation({ summary: '获取积分记录' })
  async getPointsRecord(
    @Query() dto: FindPointsLogsListDto,
    @AuthUser() user: IAuthUser
  ): Promise<Pagination<PointsLogEntity>> {
    // 获取积分记录
    return await this.pointsLogService.findByList({ ...dto, userId: user.uid });
  }

  // 兑换商品
  @Get('exchange/:id')
  @ApiOperation({ summary: '兑换商品' })
  @ApiTenantHeader()
  async exchangeGoods(
    @Param('id') goodsId: number,
    @AuthUser() user: IAuthUser,
    @AuthTenant() tenant: IAuthTenant
  ) {
    await this.pointsService.exchangePointsGoods(user.uid, tenant.id, goodsId);
  }

  @Get('exchange/check/:id')
  @ApiOperation({ summary: '校验用户是否有足够的积分可兑换' })
  @ApiTenantHeader()
  async exchangeGoodsCheck(
    @Param('id') goodsId: number,
    @AuthUser() user: IAuthUser,
    @AuthTenant() tenant: IAuthTenant
  ): Promise<{ canExchange: boolean; message: string }> {
    return await this.pointsService.exchangePointsGoodsCheck(user.uid, tenant.id, goodsId);
  }

  @Get('rank')
  @ApiOperation({ summary: '获取积分排名' })
  @ApiTenantHeader()
  async getPointsRank(@Query() dto: FindPointsRankDto, @AuthTenant() tenant: IAuthTenant) {
    // 获取积分排名
    return await this.pointsService.countTenantPointRank({ ...dto, tenantId: tenant.id });
  }

  @Get('list/exchange')
  @AllowAnon()
  @ApiOperation({ summary: '获取兑换记录' })
  async getExchangeRecord(
    @Query() dto: FindPointsGoodsExchangeListDto,
    @AuthUser() user: IAuthUser
  ) {
    // 获取兑换记录
    return await this.pointsGoodsExchangeService.getExchangeByList({ ...dto, userId: user.uid });
  }

  @AllowAnon()
  @Get('history')
  @ApiOperation({ summary: '获取过去七天的积分历史-得积分' })
  async getPointsHistory(@AuthUser() user: IAuthUser) {
    return await this.pointsService.getPointsHistory(user.uid);
  }
}
